<template>
    <input-select :id="id" :styles="styles" :options="options" isChange="Y" :index="index"   :data="data"  @input="changeData" ></input-select>
</template>
<script>
import InputSelect  from '../input/input-select'
export default {
    props:['id','styles','value','data','index','appid','exkey'],
    name: "lt-selects",
    components: {
        InputSelect
    },
     data(){
        return{
            options:[],
            oldTc:false,
            type:"",
            nameType:"",
            diffType:"",
            lt_dat1:"",
            lt_dat2:"",
            first:"Y"
        }
    },
    created(){
        this.type=this.exkey.type
        this.$post("bygex","/byg/funeral/getLtSelect",{type:this.type}).then(data=>{
            this.options=[];
            let item={optname:"",optvalue:"",sf_ex24:"是"};
            this.options.push(item)
            for(let i=1;i<data.length;i++){
                this.options.push(data[i])
            }
        })
        if(this.exkey&&this.exkey.nameType){
            this.nameType=this.exkey.nameType;
        }
        if(this.exkey&&this.exkey.diffType){
            this.diffType=this.exkey.diffType;
        }
    },
    watch:{
         data:{
            handler: function (newVal) {
                let ids=this.id.split(".");
                let pId=ids[0];
               
                let lt_dat1="";
                let lt_dat2="";
                let lt_ex3="";
                if(this.index||(this.index=='0'||this.index==0)){
                     lt_dat1=this.data[pId][this.index]["lt_dat1"];
                     lt_dat2=this.data[pId][this.index]["lt_dat2"];
                     lt_ex3=this.data[pId][this.index]["lt_ex3"];
                }else{
                    lt_dat1=this.data[pId]["lt_dat1"];
                     lt_dat2=this.data[pId]["lt_dat2"];
                     lt_ex3=this.data[pId]["lt_ex3"];
                }

                if(lt_ex3!=this.lt_ex3_old){
                    this.lt_ex3_old=lt_ex3;
                     for(let i=0;i<this.options.length;i++){
                        let sf_id=this.options[i].sf_id;
                        let optname=this.options[i].optname;
                        if(optname==lt_ex3){
                            if(this.index)
                                this.$set(this.data[pId][this.index],"lt_ex4",sf_id);
                            else
                                 this.$set(this.data[pId],"lt_ex4",sf_id);
                        }
                    }
                }
               
                if(this.first!="Y"){
                    
                    if(lt_dat1!=this.lt_dat1||lt_dat2!=this.lt_dat2){
                        //alert(2)
                        this.changeLt(lt_dat1,lt_dat2,lt_ex3)
                    }
                }else{
                    this.first="N";
                    this.lt_dat1=lt_dat1;
                    this.lt_dat2=lt_dat2;
                }
                
            },
            deep: true    //深度监听
         }
    },
    methods:{
        changeLt(lt_dat1,lt_dat2,lt_ex3){
            
            this.lt_dat1=lt_dat1;
            this.lt_dat2=lt_dat2;
            
            let days=1;
            let hours=1;
            let hours2=0;
           // alert(lt_dat1+"="+lt_dat2)
            if(lt_dat1&&lt_dat2){
                
                let lt_dat1_=lt_dat1.substring(0,10);
                
                
                let lt_dat2_=lt_dat2.substring(0,10);
                var stime =new Date(lt_dat1).getTime();
                var etime = new Date(lt_dat2).getTime();
                var usedTime = etime - stime;  //两个时间戳相差的毫秒数
                hours=Math.floor(usedTime/(3600*1000));
                hours2=usedTime/(3600*1000);


                let stime_ =new Date(lt_dat1_).getTime();
                var etime_ = new Date(lt_dat2_).getTime();
                var usedTime_ = etime_ - stime_;  //两个时间戳相差的毫秒数
                days=Math.floor(usedTime_/(24*3600*1000));

                
                //this.$emit("input",data);
                
            }
            
            for(let i=0;i<this.data["item"].length;i++){
                if(this.data["item"][i].szi_ex1==lt_ex3&&this.data["item"][i].szi_ex14==this.nameType){
                    let row_=this.data["item"][i];
                    if(row_.szi_ex28=="自然天数"&&lt_dat1&&lt_dat2)
                        days++;
                        let lt_dat1_hour=lt_dat1.substring(11,13);
                    if(row_.szi_ex28=="12点后算半天"&&lt_dat1&&lt_dat2){
                        //  alert(lt_dat1_hour)
                        if(lt_dat1_hour>12){
                            days=days+0.5;
                        }else{
                            days++;
                        }
                    }
                    if(this.data["item"][i].szi_ex3.indexOf("时")!=-1){
                        days=hours;
                        if(row_.szi_ex28=="30分钟分界线，不足按0.5算"){
                            let minute=hours2-hours;
                            if(minute<=0.5&&minute>0){
                                days=days+0.5;
                            }else if(minute>0.5){
                                days=days+1;
                            }
                        }else if(row_.szi_ex28=='向上取整'){
                            days=Math.ceil(hours2)
                        }
                    }
                        let szi_ex2=Number(row_.szi_ex2);
                    if(this.data["item"][i].szi_ex3.indexOf("场")==-1&&this.data["item"][i].szi_ex3.indexOf("次")==-1){
                        
                        row_.szi_ex4=days;
                        row_.szi_ex5=days*szi_ex2
                        this.$set(this.data["item"],i,row_);
                    }
                    
                    let sf_id=this.data["item"][i].szi_ex8;

                    if(this.data["item"][i].szi_ex3.indexOf("场")==-1&&this.data["item"][i].szi_ex3.indexOf("次")==-1){
                        this.$post("bygex","/byg/funeral/getSfItemDp",{sf_id:sf_id}).then(data=>{
                            let names=data.name.split(",");
                            for(let h=0;h<names.length;h++){
                            let name=names[h]
                                for(let j=0;j<this.data["item"].length;j++){
                                    if(this.data["item"][j].szi_ex1==name&&this.data["item"][j].szi_ex14==this.nameType){
                                        let row__=this.data["item"][j];
                                        let szi_ex3=row__.szi_ex3;

                                        let szi_ex2__=Number(row__.szi_ex2);
                                        if(szi_ex3.indexOf("时")!=-1){
                                            row__.szi_ex4=hours;
                                            row__.szi_ex5=hours*szi_ex2__
                                        }else{
                                            row__.szi_ex4=days;
                                            row__.szi_ex5=days*szi_ex2__
                                        }
                                       
                                        this.$set(this.data["item"],j,row__);
                                    }
                                }
                            }
                        });
                    }
                    break;
                }
            }
        },
        changeData(data){
           
            console.log("changeData=======================================")
            let datas=JSON.parse(data);
            let vals=datas.label;
            datas.val=datas.label;
            let isTc=false;
            let id="";
            for(let item_ of this.options){
                if(item_.optname==vals){
                    if(item_.sf_ex24=="是"){
                        isTc=true;
                        id=item_.sf_id;
                    }
                }
            }

            let ids=this.id.split(".");
            let pId=ids[0];
            let lt_dat1="";
            let lt_dat2="";
            let hours=1;
            let days=0;
            //console.log("indexlt========11112222========"+this.index+"=="+(this.index==0)+"=="+(this.index=="0")+"="+(this.index!=""&&(this.index==0||this.index>0)));
            if(this.index!=""&&(this.index==0||this.index>0)){
                lt_dat1=this.data[pId][this.index]["lt_dat1"];
                lt_dat2=this.data[pId][this.index]["lt_dat2"];
            }else{
               // console.log("222222222222222222222222222222222")
                lt_dat1=this.data[pId]["lt_dat1"];
                lt_dat2=this.data[pId]["lt_dat2"];
            }
            if(lt_dat1&&lt_dat2){
                let lt_dat1_=lt_dat1.substring(0,10);
                let lt_dat2_=lt_dat2.substring(0,10);
                var stime =new Date(lt_dat1).getTime();
                var etime = new Date(lt_dat2).getTime();
                var usedTime = etime - stime;  //两个时间戳相差的毫秒数
                hours=Math.floor(usedTime/(3600*1000));

                let stime_ =new Date(lt_dat1_).getTime();
                var etime_ = new Date(lt_dat2_).getTime();
                var usedTime_ = etime_ - stime_;  //两个时间戳相差的毫秒数
                days=Math.floor(usedTime_/(24*3600*1000));
                
            }
           
            if(!isTc){
                datas.addType="string";
                datas.itemType=this.type;
                datas.nameType=this.nameType;
                datas.hour=hours;
                datas.day=days;
               
                this.$emit("funeralItemSelectEvent",JSON.stringify(datas))
                
            }else{
               
                 this.$post("bygex","/byg/funeral/getFuneralSfMealItem",{sId:id}).then(data=>{
                    
                    let isOk=true;
                    let delItem=[];
                
                    for(let i=0;i<this.data["item"].length;i++){
                        if(this.data["item"][i].szi_ex15.indexOf(vals)!=-1){
                            isOk=false;
                        }
                        if(this.data["item"][i].szi_ex16==this.type&&(this.data["item"][i].szi_ex15.indexOf(vals)==-1||vals=='')){
                            delItem.push(this.data["item"][i].szi_ex8)
                        }
                        
                    }

                    if(isOk){
                        if(delItem.length>0){
                            
                            for(let id of delItem){
                                let index=-1;
                                for(let i=0;i<this.data["item"].length;i++){

                                    if(this.data["item"][i].szi_ex8==id){
                                        index=i;
                                        break;
                                    }
                                }
                                if(index>=0){
                                    this.$delete(this.data["item"],index)
                                }
                            }
                            
                        }
                        if(data.length>0){
                            for(let row_ of data){
                                row_.szi_ex13="业务";
                                row_.szi_ex15=vals;
                                if(row_.siz_ex7==this.type){
                                    let szi_ex2=Number(row_.szi_ex2);
                                    row_.szi_ex4=days;
                                    row_.szi_ex5=days*szi_ex2
                                }
                                this.$set(this.data["item"],this.data["item"].length,row_);
                            }
                        }
                        
                        

                    }else{
                         for(let i=0;i<this.data["item"].length;i++){
                             if(this.data["item"][i].szi_ex7==this.type){
                                let row_=this.data["item"][i];
                                let szi_ex2=Number(row_.szi_ex2);
                                row_.szi_ex4=days;
                                row_.szi_ex5=days*szi_ex2
                                this.$set(this.data["item"],i,row_);
                                break;
                             }
                         }
                    }
                });
            }
            
            
           
        }
        
    }
}
</script>